package cn.xuguowen.user.designPatten.chain_of_responsibility.jdk.filter;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: FilterChain
 * Package: cn.xuguowen.user.designPatten.chain_of_responsibility.jdk.filter
 * Description: 责任链模式的应用场景
 *  1.javaWeb中的filter有用到过
 *  2.SpringMVC中的过滤器也有用到过 HandlerExcutionChain中包含过滤器的
 *
 * @Author 徐国文
 * @Create 2023/6/2 17:40
 * @Version 1.0
 */
public class FilterChain {

    // 存放所有的过滤器
    private List<Filter> filterList = new ArrayList<>();

    private int index = 0;

    // 添加过滤器
    public FilterChain addFilter(Filter filter) {
        filterList.add(filter);
        return this;
    }

    public void doFilter(Request request,Response response) {
        if (index == filterList.size()) {
            return;
        }

        Filter filter = filterList.get(index);
        index++;
        filter.doFilter(request,response,this);
    }
}
